package org.yn.subject.domain.handler.subject;

import org.springframework.stereotype.Component;
import org.yn.subject.common.enums.SubjectInfoTypeEnum;
import org.yn.subject.domain.convert.RadioSubjectConverter;
import org.yn.subject.domain.entity.SubjectInfoBO;
import org.yn.subject.infra.basic.entity.SubjectRadio;
import org.yn.subject.infra.basic.service.SubjectRadioService;

import javax.annotation.Resource;
import java.util.LinkedList;
import java.util.List;

/**
 * 单选策略类
 */
@Component
public class RadioTypeHandler implements SubjectTypeHandler {

  @Resource
  private RadioSubjectConverter radioSubjectConverter;
  @Resource
  private SubjectRadioService subjectRadioService;

  @Override
  public SubjectInfoTypeEnum getHandlerType() {
    return SubjectInfoTypeEnum.RADIO;
  }

  @Override
  public void add(SubjectInfoBO subjectInfoBO) {

    //单选题目的插入
    List<SubjectRadio> subjectRadioList = new LinkedList<>();

    subjectInfoBO.getOptionList().forEach(option -> {
      SubjectRadio subjectRadio = radioSubjectConverter.toSubjectRadio(option);
      subjectRadio.setSubjectId(subjectInfoBO.getId());
      subjectRadioList.add(subjectRadio);
    });
    subjectRadioService.saveBatch(subjectRadioList);


  }
}
